package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.group.KuangMatchGroup;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class KuangMatchGroupDaoImpl extends TempDataObjectDao<KuangMatchGroup> {

    public static String getKuangMatchGroupMapSql = "select * from `t_c_kuang_match_group` where weekId = ? ";

    public Map<Integer, KuangMatchGroup> getKuangMatchGroupMap(int weekId) {
        Map<Integer, KuangMatchGroup> data = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getKuangMatchGroupMapSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                KuangMatchGroup parse = parse(rs);
                data.put(parse.getGroupId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }


    public static String insertKuangMatchGroupDataSql = "insert into `t_c_kuang_match_group`(weekId,groupId,openDay,serverIdList,crossId,addTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(KuangMatchGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(data.getServerIdList()))));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCrossId()));
        para.put(i++, new DbParameter(Types.DATE, data.getAddTime()));
        if (getDb().executeNoneQuery(insertKuangMatchGroupDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateKuangMatchGroupDataSql = "update `t_c_kuang_match_group` set openDay=?,serverIdList=?,crossId=?,addTime=? where weekId=? and groupId=?";

    @Override
    protected boolean updateData(KuangMatchGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(data.getServerIdList()))));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCrossId()));
        para.put(i++, new DbParameter(Types.DATE, data.getAddTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateKuangMatchGroupDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected KuangMatchGroup parseData(ResultSet rs) throws SQLException {
        KuangMatchGroup matchGroup = new KuangMatchGroup();
        matchGroup.setWeekId(rs.getInt("weekId"));
        matchGroup.setGroupId(rs.getInt("groupId"));
        matchGroup.setOpenDay(rs.getInt("openDay"));
        String serverIdList = rs.getString("serverIdList");
        if (!StringUtils.isNullOrEmpty(serverIdList)) {
            matchGroup.setServerIdList(JSON.parseObject(serverIdList, new TypeReference<List<Long>>() {
            }));
        }
        matchGroup.setCrossId(rs.getInt("crossId"));
        matchGroup.setAddTime(rs.getDate("addTime"));
        return matchGroup;
    }
}
